/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

Panels.Commodities = {

  Planets: false,
  Ticks: 1,
  Selected: false,
  
  Display: function()
  {
    this.Planets = $("CommoditiesIncludePlanets").checked;
    this.Selected = $("CommoditiesOnlySelected").checked;

    var objects = [];

    if (this.Selected)
    {
      if (this.Planets)
        objects = Utility.Object.GetAllSelectedObjects();

      else
        objects = Utility.Object.GetSelected(eBuilding);
    }
    else
    {
      if (this.Planets)
        objects = Utility.Object.GetAllObjects();

      else
        objects = Utility.Object.GetList(eBuilding);
    }

    var upkeep = {};
    var production = {};
    var balance = {};
    Global.Commodities.each(function(value, commodity) { upkeep[commodity] = 0; production[commodity] = 0; });

    for (var i = 0; i < objects.length; i++)
    {
      var object = objects[i];
      object.Upkeep().each(function(num, commodity) { upkeep[commodity] += num * this.Ticks; }, { Ticks: this.Ticks });
      object.Production().each(function(num, commodity) { production[commodity] += num * this.Ticks; }, { Ticks: this.Ticks });
    }

    var html = [];

    Global.Commodities.each(function(commodity, name)
    {
      if (upkeep[name] != 0 || production[name] != 0)
      {
        balance[name] = production[name] - upkeep[name];

        html.push("<tr class='infoTableColor" + (html.length % 2 == 1 ? "1" : "2") + "'>"
              + "<td><img src='" + Utility.Config.Get("Imagepack") + "/res/" + commodity.image + "'></td>"
              + "<td>" + name + "</td>"
              + "<td style='color: " + (production[name] != 0 ? "green" : "") + ";'>" + production[name].addCommas() + "</td>"
              + "<td style='color: " + (upkeep[name] != 0 ? "red" : "") + ";'>" + upkeep[name].addCommas() + "</td>"
              + "<td style='color: " + (balance[name] > 0 ? "green" : (balance[name] < 0 ? "red" : (upkeep[name] != 0 && production[name] != 0 ? "yellow" : ""))) + ";'>" + balance[name].addCommas() + "</td>"
              + "</tr>");
      }
    });

    var w = "<table class='commodityTable'>";

    if (html.length > 0)
    {

      w += "<tr class='commodityHeading'>"
         + "<th colspan='2' align='center'>Commodity</th>"
         + "<th align='center'>Produced</th>"
         + "<th align='center'>Consumed</th>"
         + "<th align='center'>Balance</th></tr>";
      w += html.join("");
    }

    else
    {
      w += "<tr><td colspan='5' align='center'>No Data to Display</td></tr>";
    }

    w += "</table>";

    $("CommodityTableArea").set("html", w);
  }
};

MainTabControl.addEvent("active", function(id, section, tab)
{
  if (section.get("id") == "CommoditiesTab")
    Panels.Commodities.Display();
});

CommodityTabControl.addEvent("active", function(id, section, tab)
{
  var ticks = 1;

  switch (tab.get("id").replace("Commodities", ""))
  {
    case "Tick": ticks = 1; break;
    case "Day": ticks = 4; break;
    case "Week": ticks = 28; break;
  }

  Panels.Commodities.Ticks = ticks;
  Panels.Commodities.Display();
});